package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.ConservationStatus;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QConservationStatus;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = ConservationStatusRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = ConservationStatusRepositoryCustom.class)
@Transactional(readOnly = true)
public class ConservationStatusRepositoryImpl extends QueryDslRepositorySupportExt<ConservationStatus> implements ConservationStatusRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    ConservationStatusRepositoryImpl() {
        super(ConservationStatus.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String STATUS = "status";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM = "organism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated method documentation
     *
     * @param organism
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<ConservationStatus> findByOrganism(Organism organism, GlobalSearch globalSearch, Pageable pageable) {
        QConservationStatus conservationStatus = QConservationStatus.conservationStatus;
        JPQLQuery<ConservationStatus> query = from(conservationStatus);
        Assert.notNull(organism, "organism is required");
        query.where(conservationStatus.organism.eq(organism));
        Path<?>[] paths = new Path<?>[] { conservationStatus.organism, conservationStatus.name, conservationStatus.status };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(ORGANISM, conservationStatus.organism).map(NAME, conservationStatus.name).map(STATUS, conservationStatus.status);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, conservationStatus);
    }
}
